package com.ruoyi.central.common.domain;

import com.ruoyi.central.domain.TopCentralProductType;
import com.ruoyi.common.core.domain.Tree;
import com.ruoyi.central.enums.ProductMaterialType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.EnumUtils;
import org.apache.commons.compress.utils.Sets;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @Classname TopCentralProductTypeUtils
 * @Author: sunluquan
 * @Date: 2022/4/17 21:17
 * @Description TODO
 */
public class TopCentralProductTypeUtils {


    /**
     * 获取所有的同一个类型列表
     * @param rootProduct 产品树
     * @param productMaterialType 产品类型
     * @return
     */
    public static Set<TopCentralProductType> filterProductTypeList(@NotNull Tree<TopCentralProductType> rootProduct,@NotNull ProductMaterialType productMaterialType){

        if(EnumUtils.equals(productMaterialType, rootProduct.getSource().getProductMaterialTypeId())){

            return Sets.newHashSet(rootProduct.getSource());
        }

        Set<TopCentralProductType> productTypes = new HashSet<>();

        recursiveSearch(productTypes,rootProduct.getChild(),productMaterialType);

        return productTypes;
    }

    /**
     * 递归查找指定类型 产品数据
     * @param productTypes
     * @param child
     * @param productMaterialType
     */
    private static void recursiveSearch(Set<TopCentralProductType> productTypes,List<Tree<TopCentralProductType>> child,ProductMaterialType productMaterialType){

        if(StringUtils.isEmpty(child)){
            return ;
        }
        for (Tree<TopCentralProductType> productTypeTree : child) {
            if(EnumUtils.equals(productMaterialType, productTypeTree.getSource().getProductMaterialTypeId())){
                productTypes.add(productTypeTree.getSource());
            }
            recursiveSearch(productTypes,productTypeTree.getChild(),productMaterialType);
        }

    }
}
